using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

using SypConst.Garbage;
using SypGui.CreateForms;
using SypEngine;

using SypConst.Path;

namespace SypGui.CreateForms
{
    public partial class CreationFirstStep : SypUserControl
    {
        private void InitializeSkin()
        {
            SypStep1DesignCombo.Items.Clear();
            string design_folder = SypPathHandler.GetPath(SypPathMode.LOCAL, SypPathRef.DESIGN_FOLDER, null);
            string[] DesignPath = Directory.GetDirectories(design_folder);
            foreach (string design in DesignPath)
            {
                if (SypStep1DesignCombo != null)
                {
                    string[] ThumbnailPath = Directory.GetFiles(design);
                    ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(CreationFirstStep));
                    if (ThumbnailPath.Length > 0)
                    {
                        FileInfo Thinfo = new FileInfo(ThumbnailPath[0]);
                        string skin_thumbnail = SypPathHandler.GetPath(SypPathMode.LOCAL, SypPathRef.SKIN_THUMBNAIL_NAME , null);
                        if (Thinfo.Name.Equals(skin_thumbnail))
                        {
                            SypStep1PreviewBox.Image = Image.FromFile(ThumbnailPath[0]);
                        }
                        else
                            SypStep1PreviewBox.Image = ((System.Drawing.Image)(resources.GetObject("SypStep1PreviewBox.Image")));
                        SypGarbage.free(Thinfo);
                    }
                    else
                        SypStep1PreviewBox.Image = ((System.Drawing.Image)(resources.GetObject("SypStep1PreviewBox.Image")));
                    SypGarbage.free(resources);

                   
                    DirectoryInfo dinfo = new DirectoryInfo(design);
                    bool isHidden = ((File.GetAttributes(design) & FileAttributes.Hidden) == FileAttributes.Hidden);
                    if (!isHidden)
                        SypStep1DesignCombo.Items.AddRange(new object[] { dinfo.Name });
                    SypGarbage.free(dinfo);
                }
            }
        }



        private bool ControlField()
        {
            if (SypStep1TitleText.Text == GetRessources("WizardStep1Title") || SypStep1TitleText.Text == "")
            {
                SypStep1TitleText.ForeColor = Color.Red;
                SypStep1TitleText.Text = GetRessources("WizardStep1ControlTitle");
                return false;
            }
            if (SypStep1DesignCombo.Text == GetRessources("WizardStep1Skin"))
            {
                SypStep1DesignCombo.ForeColor = Color.Red;
                SypStep1DesignCombo.Text = GetRessources("WizardStep1ControlSkin");
                return false;
            }
            return true;
        }

        public override void Reset()
        {
            SypStep1CategoryPathText.Text = GetRessources("WizardStep1CategoryPath");
            SypStep1DesignCombo.SelectedIndex = 0;
            SypStep1TitleText.Text = GetRessources("WizardStep1Title");
        }

        public CreationFirstStep()
            : base ()
        {
            InitializeSkin(); 
        }

        protected override void InitializeRessources()
        {
            base.InitializeRessources();
            //SypStep1CategoryPathText.Text = GetRessources("WizardStep1CategoryPath");

            SypStep1CategoryPathText.Text = @"C:\Documents and Settings\pulco\Mes documents\photos";
            SypStep1TitleText.Text = GetRessources("WizardStep1Title");
            SypStep1DesignCombo.Text = GetRessources("WizardStep1Skin");
            SypStep1NextLabel.Text = GetRessources("WizardButtonNext");
            SypStep1GeneralGroup.Text = GetRessources("WizardGeneralSettings");
            SypStep1HostnameLabel.Text = GetRessources("WizardStep1Hostname");
        }

        protected override void InitializeUC()
        {
            base.InitializeUC();
        }

        public CreationFirstStep(SetText outputMethod)
            :base (outputMethod)
        {
            InitializeSkin();
           // SypForm.evt_onReturnValueReceived += new SypForm.onReturnValueReceived(ProcessOnSiteIdReceived);
        }
        


        #region events

        private void SypStep1TitleText_Enter(object sender, EventArgs e)
        {
            SypStep1TitleText.ForeColor = Color.Black;
            SypStep1TitleText.Text = "";
        }

        private void SypStep1ChooseCategoryButton_Enter(object sender, EventArgs e)
        {
            SypStep1TitleText.ForeColor = Color.Black;
            SypStep1ChooseCategoryButton.Text = "";
        }

        private void SypStep1ChooseCategoryButton_Click(object sender, EventArgs e)
        {
            SypStep1TitleText.ForeColor = Color.Black;
            SypStep1CategoryPathText.Text = "";
            if (SypStep1ChooseCategoryDialog.ShowDialog() != DialogResult.OK)
            {
                SypStep1CategoryPathText.Text = GetRessources("WizardStep1CategoryPath");
                Directory.SetCurrentDirectory(Application.StartupPath);
                return;
            }
            else
            {
                SypStep1CategoryPathText.Text = SypStep1ChooseCategoryDialog.SelectedPath;

                Directory.SetCurrentDirectory(Application.StartupPath);
            }
        }

        private void SypStep1NextLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (ControlField())
            {
                ((SypContainerForm)Parent).GetNextStepUC(true);

                //add-site
                string cmd = "Add-site" + SypGarbage.COMMAND_SEPARATOR + "\"" + SypStep1TitleText.Text + "\"" + SypGarbage.COMMAND_SEPARATOR + SypStep1DesignCombo.SelectedItem.ToString() + SypGarbage.COMMAND_SEPARATOR + "\"" + ((SypStep1HostnameTextbox.Text.Equals(SypGarbage.HTTP_PREFIX)) ? "" : SypStep1HostnameTextbox.Text) + "\"";
                OutputMethod(cmd, Output.OUT);
                OutputMethod(cmd, Output.COMMAND);

                if (SypStep1CategoryPathText.Text != GetRessources("WizardStep1CategoryPath"))
                {
                    cmd = "Addfromfile-category" + SypGarbage.COMMAND_SEPARATOR + SiteId + SypGarbage.COMMAND_SEPARATOR + "\"" + SypStep1CategoryPathText.Text + "\"" + SypGarbage.COMMAND_SEPARATOR + "\"" + "\"";
                    OutputMethod(cmd, Output.OUT);
                    OutputMethod(cmd, Output.COMMAND);
                }
            }
        }


        private void SypStep1PrevLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ((SypContainerForm)Parent).GetPreviousStepUC();
        }


        private void SypStep1DesignCombo_SelectedIndexChanged(object sender, EventArgs e)
        {
            SypStep1DesignCombo.ForeColor = Color.Black;
            ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(CreationFirstStep));
            //string ThbPath = SypGarbage.DESIGN_FOLDER + SypStep1DesignCombo.SelectedItem.ToString() +"/" + SypGarbage.SKIN_THUMBNAIL;
            string ThbPath = SypPathHandler.GetPath(SypPathMode.LOCAL, SypPathRef.SKIN_THUMBNAIL_PATH, SypStep1DesignCombo.SelectedItem.ToString());
            if (File.Exists(ThbPath))
            {
                 SypStep1PreviewBox.Image = Image.FromFile(ThbPath);
            }
            else
                SypStep1PreviewBox.Image = ((System.Drawing.Image)(resources.GetObject("SypStep1PreviewBox.Image")));
            SypGarbage.free(ThbPath);
            SypGarbage.free(resources);

        }


        private void SypStep1DesignCombo_Click(object sender, EventArgs e)
        {
            SypStep1DesignCombo.ForeColor = Color.Black;
        }

        #endregion


    }
}
